import QueueInterface from './QueueInterface'
import Job from '../job/Job';
import { QueueConfig, FromQueueConfig } from '../types/index';

export default class Queue implements QueueInterface {
  private jobs: Job[] = []
  private name: string // queue name
  private target: any // 抓取类的实例对象
  private method: string // decorator挂载的方法
  private parallel: number // 任务并行数配置
  private running: boolean = false // 系统启动后该队列是否处于工作状态
  private curParallel: number = 0 // 当前并行数
  private description: string = '' // 任务描述
  private successSize: number = 0 // 成功数量
  private failSize: number = 0 // 失败数量

  constructor (config: FromQueueConfig) {
    this.name = config.name
    this.target = config.target
    this.method = config.method
    this.parallel = config.parallel
  }

  isEmpty() {
    return this.jobs.length === 0
  }

  pop () {
    return this.jobs.pop()
  }

  shift () {
    return this.jobs.shift()
  }

  peek () {
    return this.jobs.length === 0 ? null : this.jobs[0]
  }

  push (job) {
    this.jobs.push(job)
  }

  size () {
    return this.jobs.length
  }

}